/*
* @Author: LYD
* @Date:   2022-02-10
* @Last Modified by:   LYD
* @Describe: axios封装：请求拦截、响应拦截、错误统一处理
*/
import Vue from 'vue'
import axios from 'axios'
import router from '@/router'
import {clearLoginInfo} from '@/utils'
import {setLocalStorage, getLocalStorage} from './localstore.js'

import qs from 'qs'
import merge from 'lodash/merge'

import {
    Message,
    Loading
} from 'element-ui'
/**
 * 创建axios实例
 */
/**
 * 创建axios实例
 */
var instance = axios.create({timeout: 1000 * 60 * 40})     // 请求超时时长10秒
// 设置post请求头
instance.defaults.headers.post['Content-Type'] = 'application/json'
instance.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
const BASE_URL = process.env.NODE_ENV !== 'production' ? process.env.VUE_APP_BASE_API : process.env.VUE_APP_SERVER_URL
const WIN_XIN_MARK = process.env.NODE_ENV !== 'production' ? process.env.WIN_XIN_MARK : process.env.WIN_XIN_MARK
// 对面暴露的基础请求路径
instance.BASE_URL = BASE_URL
/**
 * 请求拦截器
 * 每次请求前，如果存在token则在请求头中携带token
 */
instance.interceptors.request.use(config => {
    config.headers = {
        'Content-Type': 'application/json'
    }
    if (!config.isData) {
        config.url = BASE_URL + '/weight/request/'
    } else {
        config.url = BASE_URL + '/weight/dataRequest/'
    }
    config.data = {
        opt: config.opt,
        params: config.data,
        spname: config.spname,
        weixinMark: WIN_XIN_MARK
    }
    // 系统token
    if (config.token) {
        // config.data.token = Vue.cookie.get('token')
        config.data.token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ3ZWlnaHRVc2VyIjoiezk4MTg1QjAzLTU1M0UtNDZENi1BMkI1LTQ3OEZEMzBDRTc0RX0iLCJjb21wYW55SWQiOjkzMzY2MzA2OTE0MDI5OTc3NiwiY29tcGFueVNuIjoiOTJSVTFYTk0iLCJsb2dpblR5cGUiOiI0IiwiZXhwIjoxNjQzMjc5ODU0LCJ1c2VybmFtZSI6IjI4ODAwMDAwMDAzIn0.r3S3U-_SnpTGlcv5eEBh_6sKx3M5ISbaAWRxyaERosw'
    }
    return config
}, error => {
    console.error('axios请求拦截器错误报告：\n', error)
    return Promise.reject(error)
})

/**
 * 响应拦截器
 */
instance.interceptors.response.use(
    // 响应成功
    res => {
        if (res.status === 200) {
            return Promise.resolve(res.data)
        }
    },
    // 响应失败
    error => {
        const {response} = error
        console.error('axios响应拦截器错误报告：\n', response)
        if (response) {

        } else {
            // 处理断网的情况
            /* tip('error', '网络断开，请检查网络状态！', () => {
              if (sessionStorage.student) {
                // toStuLogin();
              } else {
                toLogin();
              }
            }); */
        }
        return Promise.reject(response)
    }
)

export default instance
